// 抽象 修改
export function baseUpdate (this_, path, parameter, initTable = () => { }) {
  return new Promise((resolve) => {
    this_.$$post(path, parameter).then((resp) => {
      if (resp && resp.success === true) {
        this_.$message({
          type: 'success',
          message: resp.message
        })
        initTable()
        resolve(true)
      } else {
        this_.$message({
          type: 'warning',
          message: resp.message
        })
        resolve(false)
      }
    }).catch(() => {
      this_.disabledBtn = false
    })
  })
}

// 抽象 保存
export function baseSave (this_, path, parameter, initTable = () => { }) {
  return new Promise((resolve) => {
    this_.$$post(path, parameter).then((resp) => {
      this_.disabledBtn = false
      if (resp && resp.success === true) {
        this_.$message({
          type: 'success',
          message: resp.message
        })
        initTable()
        resolve(true)
      } else {
        this_.$message({
          type: 'warning',
          message: resp.message
        })
        resolve(false)
      }
    }).catch(() => {
      this_.disabledBtn = false
    })
  })
}

// 抽象 批量删除
export function baseBatchDelete (this_, path, ids, initTable = () => { }) {
  return new Promise((resolve) => {
    this_.$confirm('此操作将永久删除对应的记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this_.$$post(path, { ids: ids }).then((resp) => {
        if (resp && resp.success === true) {
          this_.$message({
            type: 'success',
            message: resp.message
          })
          initTable()
          resolve(true)
        } else {
          this_.$message({
            type: 'warning',
            message: resp.message
          })
          resolve(false)
        }
      })
    }).catch(() => {
      this_.$message({
        type: 'info',
        message: '已取消操作'
      })
    })
  })
}

// 抽象 删除 get
export function baseRemove (this_, path, initTable = () => { }) {
  return new Promise((resolve) => {
    this_.$confirm('此操作将永久删除对应的记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this_.$$get(path).then((resp) => {
        if (resp && resp.success === true) {
          this_.$message({
            type: 'success',
            message: resp.message
          })
          initTable()
          resolve(true)
        } else {
          this_.$message({
            type: 'warning',
            message: resp.message
          })
          resolve(false)
        }
      })
    }).catch(() => {
      this_.$message({
        type: 'info',
        message: '已取消操作'
      })
    })
  })
}
// 抽象 删除 post
export function baseRemoved (this_, path, parameter, initTable = () => { }) {
  return new Promise((resolve) => {
    this_.$confirm('此操作将永久删除对应的记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this_.$$post(path, parameter).then((resp) => {
        if (resp && resp.success === true) {
          this_.$message({
            type: 'success',
            message: resp.message
          })
          initTable()
          resolve(true)
        } else {
          this_.$message({
            type: 'warning',
            message: resp.message
          })
          resolve(false)
        }
      })
    }).catch(() => {
      this_.$message({
        type: 'info',
        message: '已取消操作'
      })
    })
  })
}

// 抽象 操作对象 新增还是编辑
export function getOperation (title = '') {
  return {
    type: `新增${title}`, // 默认新增
    value: {
      add: `新增${title}`, modify: `编辑${title}`
    }
  }
}

// 设置当前操作类型
export function setOperation (this_, operation) {
  return new Promise((resolve) => {
    if (operation === this_.operation.value.add) {
      this_.operation.type = this_.operation.value.add
    } else {
      this_.operation.type = this_.operation.value.modify
    }
    resolve(true)
  })
}
